import { _decorator, Component, Node, UITransform, Vec3 } from "cc";
const { ccclass, property, type } = _decorator;

import { gameGlobalStatus } from "./GameGlobalStatus";

@ccclass("SkyScript")
export class SkyScript extends Component {
  @type([Node])
  skies: Node[] = [];

  _skyHeight: number = 0;

  onLoad() {
    this._skyHeight = this.skies[0].getComponent(UITransform).height;

    for (let i = 1; i < this.skies.length; i++) {
      this.skies[i].setPosition(this.skies[i].position.x, this._skyHeight * i);
    }
  }

  update(deltaTime: number) {
    if (gameGlobalStatus.gameStatus == 0) {
      for (let i = 0; i < this.skies.length; i++) {
        if (this.skies[i].position.y <= this._skyHeight * -1) {
          this.skies[i].setPosition(
            this.skies[i].position.x,
            this.skies[i + 1 < this.skies.length ? i + 1 : 0].position.y +
              this._skyHeight
          );
        }

        this.skies[i].setPosition(
          this.skies[i].position.x,
          this.skies[i].position.y - gameGlobalStatus.skyMoveSpeed * deltaTime
        );
      }
    }
  }
}
